package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.Room;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IRoomService;
import com.zzyl.nursing.vo.BedVo;
import com.zzyl.nursing.vo.DeviceInfo;
import com.zzyl.nursing.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    /**
     * 根据房间id查询房间数据(楼层、房间、价格)
     * @param id
     * @return
     */
    @Override
    public RoomVo getRoomById(Integer id) {
        return roomMapper.getRoomById(id);
    }


    /**
     * 获取所有房间（智能床位与数据）
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {
        // 查询到的房间、床位设备信息是没有设备数据
        List<RoomVo> roomVoList = roomMapper.getRoomsWithDeviceByFloorId(floorId);
        // 要从redis中取设备最新数据
        if(CollUtil.isNotEmpty(roomVoList)){
            roomVoList.forEach(roomVo -> {
                // 得到每个房间，房间下有设备
                List<DeviceInfo> deviceVos = roomVo.getDeviceVos();
                setDeviceData(deviceVos);
                // 房间床位下的设备信息
                List<BedVo> bedVoList = roomVo.getBedVoList();
                if(CollUtil.isNotEmpty(bedVoList)){
                    bedVoList.forEach(bedVo -> {
                        //床绑定了设备
                        setDeviceData(bedVo.getDeviceVos());
                    });
                }
            });

        }
        return roomVoList;
    }

    /**
     * 给设备设置最新的上报数据
     * @param deviceVos
     */
    private void setDeviceData(List<DeviceInfo> deviceVos) {
        if(CollUtil.isNotEmpty(deviceVos)){
            // 从redis中取数据，并设置到设备信息中
            deviceVos.forEach(deviceVo -> {
                // 从redis中取数据，并设置到设备信息中
                String iotId = deviceVo.getIotId();
                String jsonString = (String)stringRedisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, iotId);
                // redis中有设备的数据才转
                if(StrUtil.isNotEmpty(jsonString)) {
                    List<DeviceData> dataList = JSONUtil.toList(jsonString, DeviceData.class);
                    deviceVo.setDeviceDataVos(dataList);
                }
            });
        }
    }
}
